Add meta=userinfo&uiprop=latestcontrib
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialWatchlistTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @author Addshore
7 *
8 * @group Database
9 *
10 * @covers SpecialWatchlist
11 */
12 class SpecialWatchlistTest extends SpecialPageTestBase {
13 public function setUp() {
14 parent::setUp();
15
16 $this->setTemporaryHook(
17 'ChangesListSpecialPageQuery',
18 null
19 );
20
21 $this->setMwGlobals(
22 'wgDefaultUserOptions',
23 [
24 'extendwatchlist' => 1,
25 'watchlistdays' => 3.0,
26 'watchlisthideanons' => 0,
27 'watchlisthidebots' => 0,
28 'watchlisthideliu' => 0,
29 'watchlisthideminor' => 0,
30 'watchlisthideown' => 0,
31 'watchlisthidepatrolled' => 0,
32 'watchlisthidecategorization' => 1,
33 'watchlistreloadautomatically' => 0,
34 'watchlistunwatchlinks' => 0,
35 ]
36 );
37 }
38
39 /**
40 * Returns a new instance of the special page under test.
41 *
42 * @return SpecialPage
43 */
44 protected function newSpecialPage() {
45 return new SpecialWatchlist();
46 }
47
48 public function testNotLoggedIn_throwsException() {
49 $this->setExpectedException( UserNotLoggedIn::class );
50 $this->executeSpecialPage();
51 }
52
53 public function testUserWithNoWatchedItems_displaysNoWatchlistMessage() {
54 $user = new TestUser( __METHOD__ );
55 list( $html, ) = $this->executeSpecialPage( '', null, 'qqx', $user->getUser() );
56 $this->assertContains( '(nowatchlist)', $html );
57 }
58
59 /**
60 * @dataProvider provideFetchOptionsFromRequest
61 */
62 public function testFetchOptionsFromRequest( $expectedValues, $preferences, $inputParams ) {
63 $page = TestingAccessWrapper::newFromObject(
64 $this->newSpecialPage()
65 );
66
67 $context = new DerivativeContext( $page->getContext() );
68
69 $fauxRequest = new FauxRequest( $inputParams, /* $wasPosted= */ false );
70 $user = $this->getTestUser()->getUser();
71
72 foreach ( $preferences as $key => $value ) {
73 $user->setOption( $key, $value );
74 }
75
76 $context->setRequest( $fauxRequest );
77 $context->setUser( $user );
78 $page->setContext( $context );
79
80 $page->registerFilters();
81 $formOptions = $page->getDefaultOptions();
82 $page->fetchOptionsFromRequest( $formOptions );
83
84 $this->assertArrayEquals(
85 $expectedValues,
86 $formOptions->getAllValues(),
87 /* $ordered= */ false,
88 /* $named= */ true
89 );
90 }
91
92 public function provideFetchOptionsFromRequest() {
93 // $defaults and $allFalse are just to make the expected values below
94 // shorter by hiding the background.
95
96 $page = TestingAccessWrapper::newFromObject(
97 $this->newSpecialPage()
98 );
99
100 $page->registerFilters();
101
102 // Does not consider $preferences, just wiki's defaults
103 $wikiDefaults = $page->getDefaultOptions()->getAllValues();
104
105 $allFalse = $wikiDefaults;
106
107 foreach ( $allFalse as $key => &$value ) {
108 if ( $value === true ) {
109 $value = false;
110 }
111 }
112
113 // This is not exposed on the form (only in preferences) so it
114 // respects the preference.
115 $allFalse['extended'] = true;
116
117 return [
118 [
119 [
120 'hideminor' => true,
121 ] + $wikiDefaults,
122 [],
123 [
124 'hideMinor' => 1,
125 ],
126 ],
127
128 [
129 [
130 // First two same as prefs
131 'hideminor' => true,
132 'hidebots' => false,
133
134 // Second two overriden
135 'hideanons' => false,
136 'hideliu' => true,
137 'userExpLevel' => 'registered'
138 ] + $wikiDefaults,
139 [
140 'watchlisthideminor' => 1,
141 'watchlisthidebots' => 0,
142
143 'watchlisthideanons' => 1,
144 'watchlisthideliu' => 0,
145 ],
146 [
147 'hideanons' => 0,
148 'hideliu' => 1,
149 ],
150 ],
151
152 // Defaults/preferences for form elements are entirely ignored for
153 // action=submit and omitted elements become false
154 [
155 [
156 'hideminor' => false,
157 'hidebots' => true,
158 'hideanons' => false,
159 'hideliu' => true,
160 'userExpLevel' => 'unregistered'
161 ] + $allFalse,
162 [
163 'watchlisthideminor' => 0,
164 'watchlisthidebots' => 1,
165
166 'watchlisthideanons' => 0,
167 'watchlisthideliu' => 1,
168 ],
169 [
170 'hidebots' => 1,
171 'hideliu' => 1,
172 'action' => 'submit',
173 ],
174 ],
175 ];
176 }
177 }